package com.yz.admin.aop;

/**
 * Created by 西瓜都不酸 on 2019/12/15.
 */

import com.yz.admin.feign.LoginFeign;
import com.yz.admin.service.AdminService;
import com.yz.common.pojo.LogSheet;
import com.yz.common.utils.Result;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.soap.Addressing;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xf
 * @Date: 2018/11/23 21:38
 * @Description: AOP 统一日志切面类
 */
@Slf4j
// 表示这是一个切面类
@Aspect
// 将此类交给spring容器管理
@Component
public class LogAspect {
//        @Addressing
//        StringRedisTemplate stringRedisTemplate;
        @Autowired
        private AdminService adminService;

        @Autowired
        private LoginFeign loginFeign;


    LogSheet logSheet=new LogSheet();
    // 切入点  即 aop 切入的路径（被拦截的方法）
    @Pointcut("execution(public * com.yz.admin.controller..*.*(..))")
    public void aspectPoint() {
    }

    /**
     * 前置通知
     */
    // 指定切入点 与上面定义的一样
    @Before("aspectPoint()")
    public void LogRequestInfo(JoinPoint joinPoint) throws Exception {

        // 执行controller 方法之前需要记录的请求信息
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String header = request.getHeader("Authorization");

        String username = loginFeign.getUsername(header);
        logSheet.setOperator(username);
        log.info("URL: {}",request.getRequestURL());
        log.info("HTTP_METHOD: {} ", request.getMethod());
        log.info("IP: {}", request.getRemoteAddr());
        log.info("CLASS_METHOD: {}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("ARGS: {}", Arrays.toString(joinPoint.getArgs()));


//        String authorization = request.getHeader("Authorization");
//        String usernameByToken = JwtTokenUtils.getUsernameByToken(authorization);
//        logSheet.setOperator(usernameByToken);
        logSheet.setOperationDate(new Date());

        logSheet.setOperationMethod(joinPoint.getSignature().getName());


    }

    /**
     * 后置通知
     */
    @AfterReturning(returning = "Result", pointcut = "aspectPoint()")
    public void logResultVOInfo(Result Result) throws Exception {
        log.info("请求结果：" + Result.getCode() + "\t" + Result.getMessage());

            logSheet.setOperationResult(Result.getCode() + "\t" + Result.getMessage());
            logSheet.setMethodName(Result.getMethodName());

        adminService.addLog(logSheet);
        System.out.println(Result.getMessage());
    }
}
